package com.woniu.woniuproject.web;

import com.woniu.woniuproject.entity.Account;
import com.woniu.woniuproject.entity.Funding_not_matched;
import com.woniu.woniuproject.entity.Product;
import com.woniu.woniuproject.entity.User;
import com.woniu.woniuproject.service.AccountService;
import com.woniu.woniuproject.service.Funding_not_matchedService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述:<br>
 * 〈〉
 *
 * @author EDZ
 * @create 2020/11/21
 * @since 1.0.0
 */
@Controller
@RequestMapping("generate2")
public class GenerateController {
    @Autowired
    AccountService accountService;
    @Autowired
    Funding_not_matchedService funding_not_matchedService;


    @RequestMapping("/we.html")
    public String my(){
        return "we" ;
    }



    //用户开始投资
    @PostMapping("/investment2")
    @ResponseBody
    public Map generateInvestment(Product product, @RequestParam("startingAmount") String startingAmount
                                     ){

   HashMap<String, String> map = new HashMap<>();
        System.out.println(1111);//在后台进行判断投资金额
        double anInt =Double.parseDouble(startingAmount);
        if (anInt<2000){
            map.put("status","1");
            return map; //投资金额小于2000
        }
        //product 代表产品的所有信息
        //1.查询当前用户的余额
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
        Account account=accountService.qreryAccountById(user.getId());
        Double balance = account.getBalance();
        if (anInt>balance){
            map.put("status","2");
            return map; //您的余额不足,请充值后在进行投资
        }
        Funding_not_matched fnm=new Funding_not_matched();
        fnm.setFinvestrecordid(user.getId());
        fnm.setFnotmatchedmoney(anInt);
        String Proid=String.valueOf(product.getProtypeid());
        fnm.setFfoundingtype(Proid);
        fnm.setFislocked("1");
        fnm.setFcreatedate(new Date());
        fnm.setFstatus("0");
        fnm.setPexpectedannualincome(product.getIncomerate());
        int row=accountService.addmore(user.getId(),anInt);
        int rows=funding_not_matchedService.addmatched(fnm);
        if (rows>0&&row>0){
            map.put("status","0");
            return map;//已经投资,正在审批当中
        }
          map.put("status","3");
        return map;

    }



}
